from django.shortcuts import render
from django.http import JsonResponse, HttpResponse
from django.contrib.auth.decorators import login_required

# Create your views here.

def home(request):
    return render(request,'home.html')


def demo(request, category):
    return JsonResponse({'demo':category})


def index(request):
    return render(request,'demo/index.html')

def pricing(request):
    return render(request,'demo/pricing.html')


def player(request):
    return render(request,'demo/audio_player.html')


@login_required
def secret(request):
    return render(request,'demo/secret.html')

@login_required
def logout(request):
    logout(request)
    return redirect('home')


def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        
        from django.contrib.auth import authenticate, login
        user = authenticate(request, username=username, password=password)
        
        if user is not None:
            login(request, user)
            return JsonResponse({'status': 'success'})
        else:
            return JsonResponse({'status': 'error', 'message': 'Invalid credentials'})
    else:
        return render(request,'demo/login.html')

 

def login_page(request):
    return render(request,'demo/login.html')

